package com.ivens;

import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ivens.beans.Actividad;
import com.ivens.beans.Horario;
import com.ivens.beans.Pelicula;
import com.ivens.beans.Precio;
import com.ivens.beans.Sala;
import com.ivens.common.ImageLoader;

public class DetallePeliculaActivity extends Activity {

	private ExpandableListView lstSalas;
	private View view;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detalle_pelicula);
		String fechaSeleccionada = this.getIntent().getStringExtra(
				"fechaSeleccionada");
		setTitle("Fecha seleccionada " + fechaSeleccionada);
		Actividad actividad = (Actividad) getIntent().getSerializableExtra(
				"actividad");
		// view = getLayoutInflater()
		// .inflate(R.layout.detalle_pelicula, null);
		// RatingBar ratCalificacion = (RatingBar) view
		// .findViewById(R.id.rating);
		ImageView imgImagen = (ImageView) findViewById(R.id.detalleAcvitidad_imgImagen);
		TextView txtTitulo = (TextView) findViewById(R.id.txtTitulo);
		TextView txtDirector = (TextView) findViewById(R.id.txtDirector);
		TextView txtGenero = (TextView) findViewById(R.id.txtGenero);
		TextView txtClasificacion = (TextView) findViewById(R.id.txtClasificacion);
		TextView txtActores = (TextView) findViewById(R.id.txtActores);
		TextView txtFechaEstreno = (TextView) findViewById(R.id.txtFechaEstreno);
		TextView txtDuracion = (TextView) findViewById(R.id.txtDuracion);
		TextView txtSinopsis = (TextView) findViewById(R.id.txtSinposis);
		lstSalas = (ExpandableListView) findViewById(R.id.lstSalas);
		Pelicula act = (Pelicula) actividad;

		// ratCalificacion.setNumStars(Integer.parseInt(act.getCalificacion()));
		if (act.getImagen() != null) {
			ImageLoader imageLoader = new ImageLoader(this);
			imageLoader.DisplayImage(act.getImagen().toString(), imgImagen);
		}
		txtTitulo.setText(act.getTitulo() == null ? "" : act.getTitulo());
		txtSinopsis.setText(act.getDescripcion() == null ? "" : act
				.getDescripcion());
		txtDirector.setText(act.getDirector() == null ? "" : act.getDirector());
		txtGenero.setText(act.getGenero() == null ? "" : act.getGenero());
		txtClasificacion.setText(act.getClasificacion() == null ? "" : act
				.getClasificacion());
		txtActores.setText(act.getActores() == null ? "" : act.getActores());
		txtFechaEstreno.setText(act.getFechaEstreno() == null ? "" : act
				.getFechaEstreno());
		txtDuracion.setText(act.getDuracion() == null ? "" : act.getDuracion());
		if (act.getSalas() != null) {
			lstSalas.setAdapter(new SalasAdapter(this, act.getSalas()));
		}
//		 Utility.setListViewHeightBasedOnGroups(lstSalas);

	}

	private class SalasAdapter implements ExpandableListAdapter {

		private Context _context;
		private Sala[] _salas;

		public SalasAdapter(Context context, Sala[] salas) {
			this._context = context;
			this._salas = salas;
		}

		public boolean areAllItemsEnabled() {
			// TODO Auto-generated method stub
			return false;
		}

		public Object getChild(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return _salas[groupPosition].getHorarios()[childPosition];
		}

		public long getChildId(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return childPosition;
		}

		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			if (convertView == null) {
				LayoutInflater infalInflater = (LayoutInflater) _context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = infalInflater.inflate(
						R.layout.child_layout_simple, null);
			}
			TextView txtHorario = (TextView) convertView
					.findViewById(R.id.tvChildSimple1);
			TextView txtPrecio = (TextView) convertView
					.findViewById(R.id.tvChildSimple2);
			Horario horario = _salas[groupPosition].getHorarios()[childPosition];
			Precio precio = childPosition < _salas[groupPosition].getPrecios().length ? _salas[groupPosition]
					.getPrecios()[childPosition] : null;
			txtHorario.setText(horario.getDia().concat(":")
					.concat(horario.getHoras()));
			if (precio != null) {
				txtPrecio.setText(precio.getDia().concat(": ")
						.concat(precio.getPrecio()));
			} else {
				txtPrecio.setText("");
			}
			return convertView;
		}

		public int getChildrenCount(int groupPosition) {
			// TODO Auto-generated method stub
			return _salas[groupPosition].getHorarios().length;
		}

		public long getCombinedChildId(long groupId, long childId) {
			// TODO Auto-generated method stub
			return 0;
		}

		public long getCombinedGroupId(long groupId) {
			// TODO Auto-generated method stub
			return 0;
		}

		public Object getGroup(int groupPosition) {
			// TODO Auto-generated method stub
			return _salas[groupPosition];
		}

		public int getGroupCount() {
			// TODO Auto-generated method stub
			return _salas.length;
		}

		public long getGroupId(int groupPosition) {
			// TODO Auto-generated method stub
			return groupPosition;
		}

		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			if (convertView == null) {
				LayoutInflater infalInflater = (LayoutInflater) _context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = infalInflater.inflate(
						R.layout.group_layout_simple, null);
			}
			TextView text = (TextView) convertView
					.findViewById(R.id.tvGroupSimple);
			text.setText(_salas[groupPosition].getNombre());
			// if (groupPosition == getGroupCount() - 1) {
			// parent.requestLayout();
			// }
			return convertView;
		}

		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return true;
		}

		public boolean isChildSelectable(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return false;
		}

		public boolean isEmpty() {
			// TODO Auto-generated method stub
			return false;
		}

		public void onGroupCollapsed(int groupPosition) {
//			Utility.setListViewHeightBasedOnExpandedChilds(lstSalas,
//					groupPosition, false);
		}

		public void onGroupExpanded(int groupPosition) {
//			Utility.setListViewHeightBasedOnExpandedChilds(lstSalas,
//					groupPosition, true);
			for (int i = 0; i < _salas.length; i++) {
				if (i != groupPosition) {
					lstSalas.collapseGroup(i);
				}
			}
		}

		public void registerDataSetObserver(DataSetObserver observer) {
			// TODO Auto-generated method stub

		}

		public void unregisterDataSetObserver(DataSetObserver observer) {
			// TODO Auto-generated method stub

		}

	}
}
